package cn.zcib.film.controller;

import cn.zcib.film.anno.Record;
import cn.zcib.film.bean.Subject;
import cn.zcib.film.bean.vo.SellVO;
import cn.zcib.film.bean.vo.SubjectVO;
import cn.zcib.film.service.SubjectService;
import cn.zcib.film.util.DataGridViewResult;
import cn.zcib.film.util.SystemConstants;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;

@RestController
@RequestMapping("/subject")
public class SubjectController {
    @Autowired
    private SubjectService subjectService;

    // 项目根路径下的目录  -- SpringBoot static 目录相当于是根路径下（SpringBoot 默认）
    public final static String UPLOAD_PATH_PREFIX = "static/mainpic/";

    //添加电影
    @RequestMapping("/add")
    public String addSub(Subject subject){
        HashMap<String, Object> map = new HashMap<>();

        if (subjectService.addSub(subject)>0){
            map.put(SystemConstants.SUCCESS, true);
            map.put(SystemConstants.MESSAGE, "添加成功");
        }else {
            map.put(SystemConstants.SUCCESS, false);
            map.put(SystemConstants.MESSAGE, "添加失败");
        }
        return JSON.toJSONString(map);
    }
    //更新电影
    @RequestMapping("/update")
    public String updateSub(Subject subject){
        HashMap<String, Object> map = new HashMap<>();

        if (subjectService.updateSub(subject)>0){
            map.put(SystemConstants.SUCCESS, true);
            map.put(SystemConstants.MESSAGE, "修改成功");
        }else {
            map.put(SystemConstants.SUCCESS, false);
            map.put(SystemConstants.MESSAGE, "修改失败");
        }
        return JSON.toJSONString(map);
    }
    @RequestMapping("/del")
    public String delSub(Integer sId){
        HashMap<String, Object> map = new HashMap<>();

        if (subjectService.delSubById(sId)>0){
            map.put(SystemConstants.SUCCESS, true);
            map.put(SystemConstants.MESSAGE, "删除成功");
        }else {
            map.put(SystemConstants.SUCCESS, false);
            map.put(SystemConstants.MESSAGE, "删除失败");
        }
        return JSON.toJSONString(map);
    }

    @RequestMapping("/groupDel")
    public String groupDelSub(String sIdsJSON){
        HashMap<String, Object> map = new HashMap<>();
        List<Integer> sIds=  JSONObject.parseArray(sIdsJSON,Integer.class);
        if (subjectService.groupDelSubject(sIds)>0){
            map.put(SystemConstants.SUCCESS, true);
            map.put(SystemConstants.MESSAGE, "删除成功");
        }else {
            map.put(SystemConstants.SUCCESS, false);
            map.put(SystemConstants.MESSAGE, "删除失败");
        }
        return JSON.toJSONString(map);
    }

    /**
     * 查询电影列表
     * @return
     */
    @RequestMapping("/list")
    public DataGridViewResult findSubs(SubjectVO subjectVO){
        //设置分页信息
        PageHelper.startPage(subjectVO.getPage(),subjectVO.getLimit());
        //调用查询电影方法
        //处理查询条件
        String sDirector = subjectVO.getsDirector();
        String sActor = subjectVO.getsActor();
        if (sDirector!=null & sDirector !=""){
            subjectVO.setsDirector("%"+sDirector+"%");
        }
        if (sActor!=null & sActor !=""){
            subjectVO.setsActor("%"+sActor+"%");
        }

        List<Subject> subs = subjectService.findSubs(subjectVO);

        //创建分页对象
        PageInfo<Subject> pageInfo = new PageInfo<>(subs);

//        System.out.println("pageInfo = " + pageInfo);
        return new DataGridViewResult(pageInfo.getTotal(),pageInfo.getList());
    }
    @RequestMapping("/upload")
    public String upload(MultipartFile file) {
        Map<String, Object> map = subjectService.picUpload(file);
        return JSON.toJSONString(map);
    }

    @RequestMapping(value = "/sellMap",produces = {"application/json;charset=UTF-8"})
    public String  sellMap(){
        Map<String, Integer> map = subjectService.getSell();
        List<SellVO> list = new ArrayList<>();

        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            list.add(new SellVO(entry.getKey(),entry.getValue()));
        }
        //对list排序，冒泡排序
        for (int i = 0; i < list.size()-1; i++) {
            for (int j = 0; j < list.size()-1-i; j++) {
                SellVO pre = list.get(j);
                SellVO roar = list.get(j+1);
                //降序
                if(pre.getTotalMoney()<roar.getTotalMoney()) {
                    //暂存前一个
                    SellVO tempVO = pre;
                    //后面换前面
                    list.set(j, roar);
                    //前面换后面
                    list.set(j+1, tempVO);
                }
            }
        }
//        for (SellVO vo : list) {
//            System.out.println("vo = " + vo);
//        }
//        LSort(list,1,list.size());
        return JSON.toJSONString(list);
    }
//    public void LSort(List<SellVO> list,int low,int high){
//        if (low<high){//长度大于1
//            int pivotloc = Partition(list,low,high);
//            //将list.r[low..high]一分为二,pivotloc为枢轴元素排好序的位置
//            LSort(list,low,high-1); //对低子表递归排序
//            LSort(list,pivotloc+1, high);   //对高子表递归排序
//        }
//    }
    @RequestMapping(value = "/TopStar",produces = {"application/json;charset=UTF-8"})
    public String  TopStar(){
        List<Subject> list= subjectService.getTopStar();
        return JSON.toJSONString(list);
    }
}
